package com.zhoutao.blog.seven.pages;

import com.zhoutao.blog.seven.model.response.Book;
import com.zhoutao.blog.seven.service.BookChapterService;
import com.zhoutao.blog.seven.service.BookService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.ArrayList;
import java.util.Optional;

@Slf4j
@Controller
@RequiredArgsConstructor
public class BookPageController extends AbstractPageController {

    private final BookService bookService;

    private final BookChapterService chapterService;

    @GetMapping("/books")
    public String blog(Model model) {
        packageBaseInfo(model);
        model.addAttribute("books", bookService.list());
        return "books";
    }

    @GetMapping("/books/{bookKey}/chapter")
    public String category(@PathVariable String bookKey, Model model) {
        packageBaseInfo(model);

        Optional<Book> bookOptional = bookService.findByKey(bookKey);
        model.addAttribute("chapters", chapterService.findByBookKey(bookKey));
        model.addAttribute("books", bookService.list());
        model.addAttribute("book", bookOptional.orElse(Book.EMPTY));
        return "books/category";
    }

    @GetMapping("/books/{bookName}/{articleId}/detail")
    public String detail(@PathVariable String bookName, @PathVariable String articleId, Model model) {
        packageBaseInfo(model);
        model.addAttribute("category", new ArrayList<>());
        return "books/detail";
    }
}